//
//  GSCommonLoginViewController.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/27.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonLoginViewController.h"

#import "GSCommonLoginNavigationController.h"
//
#import "GSCommonLoginInputChooseView.h"
#import "BCRecommendCodeController.h"
#import "GSCommonFindPasswordController.h"
#import "GSCommonRegisterBindPhoneController.h"
//
#import "GSCommonLoginButton.h"

//
#import "GSLocalAuthencationView.h"

//协议控制器
#import "BCProtocolController.h"

//request
#import "BCUserLoginRequest.h"//账号密码登录
#import "BCUserFingerpringLoginRequest.h"//指纹或面容登录
//切换登录用户
#import "BCSwitchAccountController.h"

//
#import "BCUserLoginManager.h"

@interface GSCommonLoginViewController ()


@property (nonatomic, strong) UIScrollView *loginScroll;

/**
 *loginContentView
 */
@property (nonatomic, strong) UIView *loginContentView;

/**
 *GSLocalAuthencationView 指纹或面容登录的视图
 */
@property (nonatomic, strong) GSLocalAuthencationView *authView;

/**
 登录欢迎语句
 */
@property (nonatomic, strong) UILabel *loginLabel;

/**
 *GSCommonLoginInputChooseView
 */
@property (nonatomic, strong) GSCommonLoginInputChooseView *loginInputView;

/**
 *forgetPassButton
 */
@property (nonatomic, strong) QMUIButton *forgetPassButton;

/**
 *loginButton
 */
@property (nonatomic, strong) GSCommonLoginButton *loginButton;

/**
 *loginTickButton
 */
@property (nonatomic, strong) QMUIButton *loginTickButton;


/**
 *textView
 */
@property (nonatomic, strong) YYTextView *agreenTextView;


#pragma mark - tempData

/**
 *纪录一下是否有上次登录成功的用户
 */
@property (nonatomic, strong) UserInfoModel *lastLoginUserInfo;

@end

@implementation GSCommonLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [BCUserLoginManager sharedLogin];
    // 设定注册按钮
    [self.customNaviBar.rightButton setTitle:GSLoginLocalized(@"Register") forState:UIControlStateNormal];
    [self.customNaviBar.rightButton setTitleColor:GSLOGIN_THEME_COLOR forState:UIControlStateNormal];
    self.customNaviBar.rightButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightRegular];
    //
    [self setupSubviews];
    //
    [self loadLastLoginUserInfo];
    //
    __weak typeof(self) wself = self;
    self.loginInputView.inputViewInvalidBlock = ^(BOOL valid) {
        if (wself.loginButton.enabled!=valid) {
            wself.loginButton.enabled = valid;
        }
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setupSubviews {
    self.loginScroll = [UIScrollView new];
    self.loginScroll.scrollEnabled = false;
    [self.view addSubview:self.loginScroll];
    [self.loginScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view).offset(-Height_Bottom_SafeArea-Height_TabBar);
    }];
    //账户密码登录的contentView
    self.loginContentView = [UIView new];
    self.loginContentView.backgroundColor = self.view.backgroundColor;
    [self.loginScroll addSubview:self.loginContentView];
    [self.loginContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.loginScroll);
        make.width.mas_equalTo(self.loginScroll);
    }];
    [self setLoginContents];
    //是否需要支持指纹或者面容识别
    BOOL isSupportLocalAuth = [LocalAuthenticationManager sharedLocalAuth].isSupportLocalAuth;
    if (isSupportLocalAuth) {
        self.authView = [GSLocalAuthencationView new];
        self.authView.backgroundColor = self.view.backgroundColor;
        [self.loginScroll addSubview:self.authView];
        [self.authView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.loginContentView.mas_right);
            make.centerY.mas_equalTo(self.loginContentView);
            make.width.mas_equalTo(self.loginContentView);
            make.height.mas_equalTo(self.loginScroll);
        }];
        //
        [self.authView.moreButton addTarget:self action:@selector(authViewMoreButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
        [self.authView.authButton addTarget:self action:@selector(authViewAuthButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    }
    //显示hi 欢迎登录
    self.loginLabel = [UILabel new];
    self.loginLabel.textColor = GSLOGINRGBRGBHEX(0x000000);
    self.loginLabel.font = [UIFont systemFontOfSize:25 weight:UIFontWeightMedium];
    NSString *appName = [GSCommonLoginAppInfomation appDisplayName];
    self.loginLabel.text = GSLoginLocalizedFormatString(@"Hi, Welcome To %@",appName);
    self.loginLabel.textAlignment = NSTextAlignmentCenter;
    self.loginLabel.numberOfLines = 0;
    self.loginLabel.adjustsFontSizeToFitWidth = true;
    [self.loginScroll addSubview:self.loginLabel];
    [self.loginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.bottom.mas_equalTo(self.loginInputView.mas_top).offset(-10.0);
        make.centerX.mas_equalTo(self.loginScroll);
        make.width.mas_equalTo(self.view).offset(-50.0);
    }];
    //
    [self.view layoutIfNeeded];
    //
    self.loginScroll.scrollEnabled = false;
    self.loginScroll.contentSize = CGSizeMake(self.loginScroll.frame.size.width*2, 0);
}
- (void)setLoginContents {
    CGFloat totlePadding = 0;
    CGFloat contentHeight = 0;
    //
    self.loginInputView = [GSCommonLoginInputChooseView new];
    [self.loginContentView addSubview:self.loginInputView];
    [self.loginInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.loginContentView);
        make.centerX.mas_equalTo(self.loginContentView);
        make.width.mas_equalTo(self.loginContentView);
        make.height.mas_equalTo([GSCommonLoginInputView defaultHeight]);
    }];
    contentHeight += [GSCommonLoginInputView defaultHeight];
    [self.loginInputView.moreButton addTarget:self action:@selector(loginInputViewMoreAction) forControlEvents:UIControlEventTouchUpInside];
    //
    self.forgetPassButton = [QMUIButton new];
    [self.forgetPassButton setTitle:GSLoginLocalized(@"Forgot Password?") forState:UIControlStateNormal];
    self.forgetPassButton.titleEdgeInsets = UIEdgeInsetsMake(4, 10, 10, 3);
    [self.forgetPassButton setTitleColor:GSLOGINRGBRGBHEX(0xD9D9D9) forState:UIControlStateNormal];
    self.forgetPassButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightLight];
    [self.loginContentView addSubview:self.forgetPassButton];
    [self.forgetPassButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.loginInputView.mas_bottom);
    }];
    [self.forgetPassButton addTarget:self action:@selector(forgetPassButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    //
    CGFloat loginButtonPadding = 40.0f;
    self.loginButton = [GSCommonLoginButton new];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginButton setTitle:GSLoginLocalized(@"Login") forState:UIControlStateNormal];
    self.loginButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightRegular];
    [self.loginContentView addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.loginInputView.mas_bottom).offset(loginButtonPadding);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo([GSCommonLoginButton defaultSize]);
    }];
    self.loginButton.enabled = false;
    [self.loginButton addTarget:self action:@selector(loginButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    totlePadding += loginButtonPadding;
    contentHeight += [GSCommonLoginButton defaultSize].height;
    //
    CGFloat loginTickButtonPadding = 57.0/2;
    self.loginTickButton = [QMUIButton new];
    [self.loginTickButton setImage:[UIImage imageNamed:@"gslogin_tick"] forState:UIControlStateNormal];
    [self.loginContentView addSubview:self.loginTickButton];
    [self.loginTickButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(10.0);
        make.top.mas_equalTo(self.loginButton.mas_bottom).offset(loginTickButtonPadding);
    }];
    self.loginTickButton.userInteractionEnabled = false;
    self.loginTickButton.alpha = 0;
    //
    self.agreenTextView = [YYTextView new];
    self.agreenTextView.textContainerInset = UIEdgeInsetsZero;
    self.agreenTextView.editable = false;
    self.agreenTextView.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.agreenTextView.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    [self.loginContentView addSubview:self.agreenTextView];
    [self.agreenTextView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.loginTickButton.mas_centerY).offset(-self.agreenTextView.font.lineHeight/2);
//        make.left.mas_equalTo(self.loginTickButton.mas_right).offset(10.0);
        make.top.mas_equalTo(self.loginButton.mas_bottom).offset(loginTickButtonPadding);
        make.left.mas_equalTo(self.view).offset(15.0);
        make.right.mas_equalTo(self.view).offset(-10.0);
        make.height.mas_equalTo(100);
    }];
    //使用条款
    NSMutableAttributedString *attriText = [[NSMutableAttributedString alloc] initWithString:GSLoginLocalized(@"Click login is agreed ") attributes:@{NSFontAttributeName:self.agreenTextView.font,NSForegroundColorAttributeName:self.agreenTextView.textColor}];
    NSMutableAttributedString *textViewAttri = [[NSMutableAttributedString alloc] initWithAttributedString:attriText];
    NSMutableAttributedString *attachment = nil;
    //
    QMUILinkButton *protocolButton = [QMUILinkButton new];
    NSString *appName = [GSCommonLoginAppInfomation appDisplayName];
    [protocolButton setTitle:GSLoginLocalizedFormatString(@"%@ Agreement",appName) forState:UIControlStateNormal];
    [protocolButton setTitleColor:GSLOGINRGBRGBHEX(0xEFA777) forState:UIControlStateNormal];
    protocolButton.titleLabel.font = self.agreenTextView.font;
    [protocolButton sizeToFit];
    [protocolButton addTarget:self action:@selector(protocolButtonDidClickedAction:) forControlEvents:UIControlEventTouchUpInside];
    //
    attachment = [NSMutableAttributedString yy_attachmentStringWithContent:protocolButton contentMode:UIViewContentModeLeft attachmentSize:protocolButton.frame.size alignToFont:protocolButton.titleLabel.font alignment:YYTextVerticalAlignmentCenter];
    //
    [textViewAttri appendAttributedString:attachment];
    self.agreenTextView.attributedText = textViewAttri;
    //
    contentHeight += self.agreenTextView.font.lineHeight*3;
    //
    contentHeight += totlePadding;
    //
    [self.loginContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(contentHeight);
    }];
}
#pragma mark - 启动登录界面时 检查是否有上次登录成功的用户 以及判别是否能够指纹或面容登录
- (void)loadLastLoginUserInfo {
    UserInfoModel *userInfo = [BCUserLoginManager sharedLogin].lastUserInfo;
    BOOL needAuth = false;
    if (userInfo) {
        self.lastLoginUserInfo = userInfo;
        BOOL userOpen = [[BCUserLoginManager sharedLogin] isOpenLocalAuthenticationWithUserId:userInfo.sub];
        if (userOpen) {
            needAuth = true;
            _authView.userInfo = self.lastLoginUserInfo;
        }else {
            NSString *account = userInfo.phone;
            if (!account.length) {
                account = userInfo.email;
            }
            self.loginInputView.accountTF.text = account;
        }
    }else {//说明还没有用户成功登录过
        
    }
    self.loginScroll.contentOffset = CGPointMake(needAuth?self.loginScroll.frame.size.width:0, 0);
    //如果上次登录用户支持指纹或面容登录 弹出登录
    if (needAuth) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.authView.authButton sendActionsForControlEvents:UIControlEventTouchUpInside];
        });
    }
    [self updateUIs];
}
/**
 此方法主要用于切换界面后更新一些UI
 */
- (void)updateUIs {
    UserInfoModel *userInfo = self.lastLoginUserInfo;
    NSInteger infosCount = [BCUserLoginManager sharedLogin].loginedUserInfos.count;
    if (userInfo) {
        if (infosCount<=1) {
            BOOL userOpen = [[BCUserLoginManager sharedLogin] isOpenLocalAuthenticationWithUserId:userInfo.sub];
            if (userOpen) {
                self.loginInputView.moreButton.alpha = 1;
                if (_authView) {
                    if (infosCount<=1) {
                        [self.authView.moreButton setTitle:GSLoginLocalized(@"Passwrod Login") forState:UIControlStateNormal];
                    }else {
                        [self.authView.moreButton setTitle:GSLoginLocalized(@"More") forState:UIControlStateNormal];
                    }
                }
            }else {
                self.loginInputView.moreButton.alpha = 0;
            }
        }else {
            self.loginInputView.moreButton.alpha = 1;
        }
    }else {
        self.loginInputView.moreButton.alpha = 0;
    }
}


/**
 账户输入右边下拉按钮点击
 */
- (void)loginInputViewMoreAction {
    [self.view endEditing:true];
    UserInfoModel *userInfo = self.lastLoginUserInfo;
    NSInteger infosCount = [BCUserLoginManager sharedLogin].loginedUserInfos.count;
    if (!userInfo) {
        return;
    }
    NSString *title;// = self.lastLoginUserInfo.nickname;
    if (!title.length) {
        title = userInfo.phone;
    }
    if (!title) {
        title = userInfo.email;
    }
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //
    BOOL userOpen = [[BCUserLoginManager sharedLogin] isOpenLocalAuthenticationWithUserId:userInfo.sub];
    WEAK_SELF
    if (userOpen) {
        //
        //设置 指纹登录或者面容登录
        NSString *actionTitle = @"UnKnown";
        DDLABiometryType type = [LocalAuthenticationManager sharedLocalAuth].biometryType;
        switch (type) {
            case DDLABiometryTypeFaceID:
            {
                actionTitle = GSLoginLocalized(@"Face login");
            }
                break;
            case DDLABiometryTypeTouchID:
            {
                actionTitle = GSLoginLocalized(@"Fingerprint login");
            }
            default:
                break;
        }
        UIAlertAction *authLogin = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [wself changeToAuthLogin:userInfo];
        }];
        [actionSheet addAction:authLogin];
    }
    if (infosCount>1) {
        //Switch account
        UIAlertAction *switchAccount = [UIAlertAction actionWithTitle:GSLoginLocalized(@"Switch account") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [wself loginInputViewDidClickedSwitchAccount];
        }];
        [actionSheet addAction:switchAccount];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:GSLoginLocalized(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [actionSheet addAction:cancel];
    [self.navigationController presentViewController:actionSheet animated:true completion:nil];
}
/**
 指纹或面容登录界面更多点击
 */
- (void)authViewMoreButtonDidClickedAction {
    [self.view endEditing:true];
    NSInteger infosCount = [BCUserLoginManager sharedLogin].loginedUserInfos.count;
    UserInfoModel *userInfo = self.lastLoginUserInfo;
    if (infosCount<=1) {
        [self changeToAccountLogin:userInfo];
    }else {
        if (!userInfo) {
            [self changeToAccountLogin:userInfo];
            return;
        }
        NSString *title;// = self.lastLoginUserInfo.nickname;
        if (!title.length) {
            title = userInfo.phone;
        }
        if (!title) {
            title = userInfo.email;
        }
        WEAK_SELF
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        //Passwrod Login
        UIAlertAction *passLogin = [UIAlertAction actionWithTitle:GSLoginLocalized(@"Passwrod Login") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [wself changeToAccountLogin:userInfo];
        }];
        [actionSheet addAction:passLogin];
        //Switch account
        UIAlertAction *switchAccount = [UIAlertAction actionWithTitle:GSLoginLocalized(@"Switch account") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [wself authLoginViewDidClickedSwitchAccount];
        }];
        [actionSheet addAction:switchAccount];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:GSLoginLocalized(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [actionSheet addAction:cancel];
        [self.navigationController presentViewController:actionSheet animated:true completion:nil];
    }
}
/**
 账户登录界面点击了切换账户
 */
- (void)loginInputViewDidClickedSwitchAccount {
    WEAK_SELF
    [BCSwitchAccountController showSwitchAccountFromController:self.navigationController userInfo:self.lastLoginUserInfo complete:^(UserInfoModel * _Nonnull userInfo) {
        wself.lastLoginUserInfo = userInfo;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //
            NSString *account = userInfo.phone;
            if (!account.length) {
                account = userInfo.email;
            }
            wself.loginInputView.accountTF.text = account;
        });
    }];
}
/**
 指纹或面容登录界面点击了切换账户
 */
- (void)authLoginViewDidClickedSwitchAccount {
    WEAK_SELF
    [BCSwitchAccountController showSwitchAccountFromController:self.navigationController userInfo:self.lastLoginUserInfo complete:^(UserInfoModel * _Nonnull userInfo) {
        wself.lastLoginUserInfo = userInfo;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //
            if (userInfo) {
                BOOL userOpen = [[BCUserLoginManager sharedLogin] isOpenLocalAuthenticationWithUserId:userInfo.sub];
                if (userOpen) {
                    wself.authView.userInfo = userInfo;
                    //
                    [wself.authView.authButton sendActionsForControlEvents:UIControlEventTouchUpInside];
                }else {
                    [wself changeToAccountLogin:userInfo];
                }
            }else {
                [wself changeToAccountLogin:userInfo];
            }
        });
    }];
}



#pragma mark - 登录界面之间互相切换
- (void)changeToAuthLogin:(UserInfoModel *)userInfo {
    if (_authView) {
        _authView.userInfo = userInfo;
    }
    self.loginScroll.contentOffset = CGPointMake(self.loginScroll.frame.size.width, 0);
    CATransition *transiton = [CATransition animation];
    transiton.type = kCATransitionPush;
    transiton.subtype = kCATransitionFromRight;
    transiton.removedOnCompletion = YES;
    [self.loginScroll.layer addAnimation:transiton forKey:nil];
    //
    [self updateUIs];
    //
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.authView.authButton sendActionsForControlEvents:UIControlEventTouchUpInside];
    });
}
- (void)changeToAccountLogin:(UserInfoModel *)userInfo {
    if (userInfo) {
        NSString *account = userInfo.phone;
        if (!account.length) {
            account = userInfo.email;
        }
        self.loginInputView.accountTF.text = account;
        //
        [self.loginInputView.passwordTF becomeFirstResponder];
    }
    //
    self.loginScroll.contentOffset = CGPointZero;
    CATransition *transiton = [CATransition animation];
    transiton.type = kCATransitionPush;
    transiton.subtype = kCATransitionFromLeft;
    transiton.removedOnCompletion = YES;
    [self.loginScroll.layer addAnimation:transiton forKey:nil];
    //
    [self updateUIs];
}



#pragma mark - authViewAcountLoginButtonDidClickedAction
/**
 切换到账户密码登录
 */
- (void)authViewAcountLoginButtonDidClickedAction {
    [self changeToAccountLogin:self.lastLoginUserInfo];
}
#pragma mark - authViewAuthButtonDidClickedAction
- (void)authViewAuthButtonDidClickedAction {
    WEAK_SELF
    [[LocalAuthenticationManager sharedLocalAuth] startLocalAuthenticationFromController:self.navigationController complete:^(BOOL success, NSError *error) {
        if (success) {
            [wself requestUserFingerprintLoginAction];
        }
    }];
}


#pragma mark -
- (void)didClickedNaviRightButton {
    [self.view endEditing:true];
    BCRecommendCodeController *codeVC = [BCRecommendCodeController new];
    [self.navigationController pushViewController:codeVC animated:true];
}
- (void)forgetPassButtonDidClickedAction {
    [self.view endEditing:true];
    GSCommonFindPasswordController *findPassVC = [GSCommonFindPasswordController new];
    [self.navigationController pushViewController:findPassVC animated:true];
}

#pragma mark - loginButtonDidClickedAction
- (void)loginButtonDidClickedAction {
    [self.view endEditing:true];
    //
    [self requestUserNameLoginAction];
}
//账户密码登录请求
- (void)requestUserNameLoginAction {
    [self.view showLoginToast];
    BCUserLoginRequest *request = [BCUserLoginRequest new];
    request.username = self.loginInputView.account;
    request.password = self.loginInputView.password;
    request.uuid = [GeneralParameterManager sharedGeneral].uuid;
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view hideLoginToast];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCUserLoginResponse *resp = result;
        if (resp.data) {
            [[BCUserManager sharedUser] userLoginSuccessUpdateUserLoginData:resp.data];
        }
        [wself userLoginSuccessAction];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [request requestServer];
}
//指纹或面容登录请求
- (void)requestUserFingerprintLoginAction {
    [self.view showLoginToast];
    BCUserFingerpringLoginRequest *request = [BCUserFingerpringLoginRequest new];
    NSString *lastLoginUserId = self.lastLoginUserInfo.sub;
    request.fingerprint = [[BCUserLoginManager sharedLogin] getCurrentUserLoacalAuthenticationCode:lastLoginUserId];
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view hideLoginToast];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCUserFingerpringLoginResponse *resp = result;
        if (resp.data) {
            [[BCUserManager sharedUser] userLoginSuccessUpdateUserLoginData:resp.data];
        }
        [wself userLoginSuccessAction];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [request requestServer];
}

- (void)userLoginSuccessAction {
    [self.navigationController dismissViewControllerAnimated:true completion:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:UserCommonLoginSuccessNotification object:nil];
    }];
}

#pragma mark - protocolButtonDidClickedAction
- (void)protocolButtonDidClickedAction:(QMUIButton *)button {
    [self.view endEditing:true];
    //需要加语言
    NSString *protocol = APP_Protocol;
    NSString *lang = [BCLabguageSettingManager shared].requestLang;
    if (lang.length) {
        protocol = [protocol stringByAppendingFormat:@"?lang=%@",lang];
    }
    [BCProtocolController showFromController:self.navigationController protocolURLString:protocol];
}

#pragma mark -
+ (void)presentToCommloginController:(UIViewController *)fatherController {
    if (fatherController) {
        GSCommonLoginViewController *loginVC = [GSCommonLoginViewController new];
        GSCommonLoginNavigationController *nav = [[GSCommonLoginNavigationController alloc] initWithRootViewController:loginVC];
        nav.navigationBar.hidden = true;
        [fatherController presentViewController:nav animated:YES completion:nil];
    }
}

@end
